import { Toast } from 'antd-mobile';
import axios from 'axios'


const instance = axios.create({
    baseURL: '/api',
    timeout: 10000,
    headers: {'X-Custom-Header': 'foobar'}
  });
// 添加请求拦截器
instance.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    Toast.show({
        content: '正在请求数据',
        duration: 0,
        icon: 'loading'
    })
    const token=localStorage.getItem('token')
    if(token){
        config.headers.Authorization=`Bearer ${token}`
    }

    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    Toast.clear()
    return response;
  }, function (error) {
    // 对响应错误做点什么
    Toast.clear()
    if (error.response) {
        const status=error.response.data
        switch(status){
            case 404:
                console.log('请求路径错误');
            break;
            case 500:
                console.log('浏览器报错');
            break;
        }
      } else if (error.request) {
        // The request was made but no response was received
        // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
        // http.ClientRequest in node.js
        console.log(error.request);
      } else {
        // Something happened in setting up the request that triggered an Error
        console.log('Error', error.message);
      }
    return Promise.reject(error);
  });
export default instance


